*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   SOEP-IAB Data 
*
*===============================================================================


********************************************************************************
*								Occupationrank 								   *
********************************************************************************

cap log close                   
log using ${log}/4_occupationrank.log, replace  
set seed 6000



*** create dataset containing SOEP individuals' wage percentiles in their occupations

set scheme s1color

* loading dataset
use prs_id ieb_beg_epi ieb_end_epi ieb_tag_entg ieb_beruf_kons_num using "$orig/coworker_fullhist_IEB.dta" if inrange(year(ieb_beg_epi),2019,2019), clear

* merging on SOEP IDs
getlink, mergevar(prs_id)

drop if _merge==2
drop _merge

gen jahr = year(ieb_beg_epi)

* imposing minimum wage of 9.19, 6 hours per day, and 250/365 working days per year in accordance to Dustmann et al. Appendix
keep if ieb_tag_entg > 9.19*6*(250/365) & ieb_tag_entg!=. 

* for each person, keeping one main 2019 spell
gen spell_earnings = (ieb_end_epi-ieb_beg_epi+1)*ieb_tag_entg
drop if spell_earnings == . | spell_earnings<0
gen sort_spell_earnings = -spell_earnings
bys prs_id jahr (sort_spell_earnings): gen priority_spell = _n

keep if priority_spell==1

* generating percentile ranks
bys ieb_beruf_kons_num (ieb_tag_entg): gen rank = _n/_N
gen ieb_beruf_kons_num_ventile = round(rank*20)

keep pid ieb_beruf_kons_num ieb_beruf_kons_num_ventile 

duplicates drop pid, force

save "$data/within_occupation_rank_2019.dta", replace


cap log close
clear
